package com.project.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.framework.core.result.BasePage;
import com.project.biz.CmsProductBiz;
import com.project.biz.CmsProductRecommendBiz;
import com.project.entity.CmsProductRecommend;
import com.project.mapper.CmsProductRecommendMapper;
import com.project.pojo.dto.ProductDTO;
import com.project.pojo.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 商品推荐业务层接口实现类
 * @Author: zhuchi
 * @CreateDate: 2020/11/20 0:27
 * @Version: 1.0
 */
@Service
public class CmsProductRecommendBizImpl extends ServiceImpl<CmsProductRecommendMapper, CmsProductRecommend> implements CmsProductRecommendBiz {

    /**
     * 商品业务层接口类
     */
    @Autowired
    private CmsProductBiz productBiz;

    /**
     * 添加推荐商品
     * @param addRecommendProductModel
     * @param userId
     * @return
     */
    public int addRecommendProduct(AddRecommendProductModel addRecommendProductModel, Long userId){
        int result = 0;
        Date nowTime = new Date();

        //获取目前最大排序值
        int maxSort = this.baseMapper.getMaxSort();
        for (Long productId : addRecommendProductModel.getProductIds()){
            maxSort = maxSort + 2;
            CmsProductRecommend productRecommend = new CmsProductRecommend();
            productRecommend.setProductId(productId);
            productRecommend.setType(addRecommendProductModel.getType());
            productRecommend.setSort(maxSort);
            productRecommend.setCreateTime(nowTime);
            productRecommend.setCreateUser(userId);
            int addProductRecommend = this.baseMapper.insert(productRecommend);
            if (addProductRecommend > 0){
                result++;
            }
        }

        return result;
    }

    /**
     * 分页查询推荐商品
     * @param recommendProductModel
     * @return
     */
    public BasePage<ProductDTO> selectRecommendProduct(GetRecommendProductModel recommendProductModel){
        Page<CmsProductRecommend> page = new Page<>(recommendProductModel.getPageNo(), recommendProductModel.getPageSize());
        IPage<CmsProductRecommend> iPage = this.baseMapper.selectRecommendProduct(page, recommendProductModel);

        List<ProductDTO> recommendProductDTOS = new ArrayList<>();

        if (null != iPage && null != iPage.getRecords() && iPage.getRecords().size() > 0){
            List<Long> productIds = new ArrayList<>();
            for (CmsProductRecommend productRecommend : iPage.getRecords()){
                productIds.add(productRecommend.getProductId());
            }

            //根据推荐商品ID集合查询商品信息
            ProductModel productModel = new ProductModel();
            productModel.setProductIds(productIds);
            productModel.setOrderById(2);
            productModel.setState(1);//状态（0:下架,1:上架）
            List<ProductDTO> productDTOS = this.productBiz.selectProductList(productModel);

            //根据ID去商品表查询时会根据ID排序,所以查询出来以后需要根据推荐表再次排序
            if (null != productDTOS && productDTOS.size() >0){
                for (CmsProductRecommend productRecommend : iPage.getRecords()){
                    for (ProductDTO productDTO : productDTOS){
                        if (!recommendProductDTOS.contains(productDTO)){
                            if (productDTO.getProductId().equals(productRecommend.getProductId())){
                                productDTO.setSort(productRecommend.getSort());
                                recommendProductDTOS.add(productDTO);
                            }
                        }
                    }
                }
            }

        }

        BasePage<ProductDTO> basePage = new BasePage<>();
        basePage.setPageNo((int)iPage.getCurrent());
        basePage.setPageSize((int)iPage.getSize());
        basePage.setPages((int)iPage.getPages());
        basePage.setTotal((int)iPage.getTotal());
        basePage.setData(recommendProductDTOS);

        return basePage;
    }

    /**
     * 查询推荐商品列表
     * @param recommendProductModel
     * @return
     */
    public List<ProductDTO> selectRecommendProductList(GetRecommendProductModel recommendProductModel){
        QueryWrapper<CmsProductRecommend> queryWrapper = new QueryWrapper<>();
        if (null != recommendProductModel.getType() && recommendProductModel.getType() > 0){
            queryWrapper.eq("type", recommendProductModel.getType());
            //去重
            queryWrapper.select("DISTINCT product_id");
        }
        queryWrapper.orderByDesc("sort");
        List<CmsProductRecommend> list = this.baseMapper.selectList(queryWrapper);

        List<ProductDTO> recommendProductDTOS = new ArrayList<>();

        if (null != list && list.size() > 0){
            List<Long> productIds = new ArrayList<>();
            for (CmsProductRecommend productRecommend : list){
                productIds.add(productRecommend.getProductId());
            }

            //根据推荐商品ID集合查询商品信息
            ProductModel productModel = new ProductModel();
            productModel.setProductIds(productIds);
            productModel.setOrderById(2);
            productModel.setState(1);//状态（0:下架,1:上架）
            List<ProductDTO> productDTOS = this.productBiz.selectProductList(productModel);

            //根据ID去商品表查询时会根据ID排序,所以查询出来以后需要根据推荐表再次排序
            for (Long productId : productIds){
                if (null != productDTOS && productDTOS.size() >0){
                    for (ProductDTO productDTO : productDTOS){
                        if (!recommendProductDTOS.contains(productDTO)){
                            if (productDTO.getProductId().equals(productId)){
                                recommendProductDTOS.add(productDTO);
                            }
                        }
                    }
                }
            }
        }

        return recommendProductDTOS;
    }

    /**
     * 删除推荐商品
     * @param delRecommendProductModel
     * @return
     */
    public int delRecommendProduct(DelRecommendProductModel delRecommendProductModel){
        QueryWrapper<CmsProductRecommend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", delRecommendProductModel.getType());
        queryWrapper.in("recommend_id", delRecommendProductModel.getRecommendIds());
        return this.baseMapper.delete(queryWrapper);
    }

    /**
     * 修改推荐商品排序值
     * @param updateRecommendProductSortModel
     * @param userId
     * @return
     */
    public int updateRecommendProductSort(UpdateRecommendProductSortModel updateRecommendProductSortModel, Long userId){
        return this.baseMapper.updateRecommendProductSort(updateRecommendProductSortModel, userId);
    }

}
